﻿using Sunny.UI;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIOrderItem : UIUserControl
    {
        public UIOrderItem()
        {
            InitializeComponent();
            Info = new OrderItemInfo();
            Text = Info.Name;
        }

        public OrderItemInfo Info { get; }

        public UIOrderItem(OrderItemInfo info)
        {
            InitializeComponent();
            Info = info;
            Text = info.Name;
        }

        private bool selected = false;
        public bool Selected
        {
            get => selected;
            set
            {
                selected = value;
                Invalidate();
            }
        }

        private Color selectedColor = Color.FromArgb(221, 221, 221);
        public Color SelectedColor
        {
            get => selectedColor;
            set
            {
                selectedColor = value;
                Invalidate();
            }
        }

        private void UIOrderItem_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (Selected)
            {
                e.Graphics.FillRectangle(SelectedColor, 0, 0, Width, Height);
            }
            if (Info == null) return;

            Size namesize = TextRenderer.MeasureText(Text, Font);
            e.Graphics.DrawString(Text, Font, ForeColor, new Rectangle(25, 17, Width, 22), ContentAlignment.MiddleLeft);

            using (Font font = new Font("微软雅黑", 10.5f))
            {
                int idx = 0;
                foreach (var item in Info.Tags)
                {
                    e.Graphics.DrawString(item, font, Color.Red, new Rectangle(25 + namesize.Width + idx * 24, 17, Width, 22), ContentAlignment.MiddleLeft);
                    e.Graphics.DrawRoundRectangle(Color.Red, new Rectangle(27 + namesize.Width + idx * 24, 19, 50, 20), 3);
                    idx++;
                }

                namesize = TextRenderer.MeasureText(Info.Order, font);
                e.Graphics.DrawString(Info.Order, font, ForeColor, new Rectangle(0, 17, Width - 10, 22), ContentAlignment.MiddleRight);
                if (Info.Returning.IsValid())
                {
                    using (Font font1 = new Font("微软雅黑", 9f))
                    {
                        e.Graphics.DrawString(Info.Returning, font1, Color.DarkGray, new Rectangle(0, 18, Width - 8 - namesize.Width, 22), ContentAlignment.MiddleRight);
                        var namesize1 = TextRenderer.MeasureText(Info.Returning, font1);
                        e.Graphics.DrawRoundRectangle(Color.DarkGray, new Rectangle(Width - 10 - namesize.Width - namesize1.Width, 19, namesize1.Width, namesize1.Height), 3);
                    }
                }

                e.Graphics.DrawString(Info.OrderID, font, ForeColor, new Rectangle(25, 49, Width, 22), ContentAlignment.MiddleLeft);

                namesize = TextRenderer.MeasureText(Info.Money, font);
                var namesize2 = TextRenderer.MeasureText(Info.MoneyFrom, font);
                if (namesize2.Width < Width - 10 - 160 - namesize.Width)
                {
                    e.Graphics.DrawString(Info.Money, font, foreColor, new Rectangle(0, 49, Width - 10, 22), ContentAlignment.MiddleRight);
                    e.Graphics.DrawString(Info.MoneyFrom, font, foreColor, new Rectangle(0, 49, Width - 10 - namesize.Width, 22), ContentAlignment.MiddleRight);
                }
                else
                {
                    e.Graphics.DrawString(Info.MoneyFrom, font, ForeColor, new Rectangle(160, 49, Width, 22), ContentAlignment.MiddleLeft);
                    e.Graphics.FillRectangle(Selected ? SelectedColor : Color.White, new Rectangle(Width - 15 - namesize.Width, 49, Width, 22));
                    e.Graphics.DrawString(Info.Money, font, foreColor, new Rectangle(0, 49, Width - 10, 22), ContentAlignment.MiddleRight);
                }

                e.Graphics.DrawLine(Color.Silver, 0, Height - 1, Width, Height - 1);
            }
        }
        public event EventHandler TableClick;

        private void UIOrderItem_Click(object sender, System.EventArgs e)
        {
            //Selected = !selected;
            Selected = true;
            foreach (var item in Parent.GetControls<UIOrderItem>())
            {
                if (item.Equals(this)) continue;

                item.Selected = false;
            }
            TableClick?.Invoke(Info, e);
        }
    }
}
